<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Ajax extends MY_Controller {
	
	private $data;
	
	public function __construct(){
		parent::__construct();
		$this->load->model('community_model');
	}
	
	public function add(){
		$post = $this->input->post(null,true);
		unset($post['submit']);
		
        foreach($post as $item=>$data){
                if($data==''){
                        $output=array(
                                'status'=>FORM_VALIDATION_ERROR,
                                'message'=>FORM_EMPTY_MESSAGE,
                        );
                        print json_encode($output);exit;
                        break;
                }
        }
        
        if(!isset($post['comm_agree_term'])){
			$output=array(
					'status'=>FORM_VALIDATION_ERROR,
					'message'=>FORM_INVALID_AGREEMENT,
			);
			print json_encode($output);exit;
			break;
		}
        
        unset($post['comm_agree_term']);
        
        $post['community_status']	= COMMUNITY_STATUS_ACTIVE;
        $post['created']			= time();
        
		if($id = $this->community_model->add_communty($post)){
			$people['community_access_id']  = OWNER_COMMUNITY_ID;
			$people['user_id'] 				= $this->users_lib->get_user_id();
			$people['community_id'] 		= $id;
			
			$this->community_model->add_community_people($people);
			
			$output['status']  = FORM_VALIDATION_SUCCEED;
			$output['message'] = base_url('community/community/index/'.$id) ;
			
			print json_encode($output);exit;
		}
	}
	
}
